//
//  main.m
//  DecoPlanner4
//
//  Created by Donald MacKay on 11-04-26.
//  Copyright 2011 __MyCompanyName__. All rights reserved.
//

#import <Foundation/Foundation.h>
#include <math.h>

#include "Dive.h"
#include "Constants.h"
#include "DecoPlanner4.h"
#include "Utilities.h"

int main (int argc, const char * argv[])
{
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    
    // Create a DivePhaseParameters object 

    GasMix *mix = [[GasMix alloc] initAsTriMixWithFO2Of:0.18 andFractionOfHelium:0.45];
    
    DivePhaseParameters *diveParams = [[DivePhaseParameters alloc] initWithStartDepth:0.0 
                                                                             endDepth:200 
                                                                           ambientATA:33 
                                                                   waterVaporPressure:kBuhlmannWVPInFSW 
                                                                          descentRate:60.0
                                                                                 Time:28.0];
  
    DivePhaseParameters *asc1DiveParams = [[DivePhaseParameters alloc] initWithStartDepth:200.0 
                                                                             endDepth:140 
                                                                           ambientATA:33 
                                                                   waterVaporPressure:kBuhlmannWVPInFSW 
                                                                          descentRate:30.0
                                                                                 Time:2.0];
    DivePhaseParameters *asc2DiveParams = [[DivePhaseParameters alloc] initWithStartDepth:200.0 
                                                                                endDepth:80 
                                                                              ambientATA:33 
                                                                      waterVaporPressure:kBuhlmannWVPInFSW 
                                                                             descentRate:30.0
                                                                                    Time:2.0];

    DecoPlanner4 *decoObj = [[DecoPlanner4 alloc] init];
    
    [decoObj calcDescentWithDiveParameters:diveParams andGasMix:mix];
 
    NSLog(@"%@", [decoObj compartmentArray]);
    
    [decoObj calcBottomTimeWithDiveParameters:diveParams andGasMix:mix];
    
    NSLog(@"%@", [decoObj compartmentArray]);
    
    [decoObj findLeadingCompartment:[[diveParams ambientATA] floatValue]];
    
    [decoObj calcBottomTimeWithDiveParameters:asc1DiveParams andGasMix:mix];

    [decoObj findLeadingCompartment:[[diveParams ambientATA] floatValue]];

    [decoObj calcBottomTimeWithDiveParameters:asc2DiveParams andGasMix:mix];
    
    [decoObj findLeadingCompartment:[[diveParams ambientATA] floatValue]];

//    Dive *diveObj = [[Dive alloc] initWithDepth:45 time:30 switchDepth:21 SAC:20 andIsMetric:TRUE];
//    
//    NSEnumerator *enumerator = [[diveObj diveProfile] objectEnumerator];
//    id element;
//    
//    while((element = [enumerator nextObject])){
//        NSLog(@"%@", element);
//    }

//    Create a Dive
//    double finalDepth = 120;                                         /* meters or feet */
//    double initialDepth = 0.0;                                          /* meters or feet */
//    double initialWaterPressure = 33;
//    double initialAmbPressMinusWaterVapor = initialWaterPressure - 2.042; //kWaterVaporPressureMSW;                        /* initial ambient water pressure minus water vapor */          
//    double descentRate = 60;                                       /* meters or feet */
//    double timeInterval = (finalDepth - initialDepth) / descentRate;    /* time of exposure */
//    double startPPN2 = 0.79;
//    double startPPHe = 0.0;
//
//    
//    double fractionOfHeInMix = 0.45;
//    double initialInspiredGasPressHe = initialAmbPressMinusWaterVapor * fractionOfHeInMix;                               /* initial inspired (alveolar) inert gas pressure in breathing mix */
//    double initialCompartmentPressHe = initialWaterPressure * startPPHe;                               /* initial compartment inert gas pressure */
//    double rateIspiredVsAbmientHe = descentRate * fractionOfHeInMix;                                  /* rate of change in inspired gas pressure with change in ambient pressure */
//    double halfTimeConstHe = log(2.0)/1.51;                                       /* half-time constant = ln2/half-time for helium */
//
//    double fractionOfN2InMix = 0.40;  
//    double initialInspiredGasPressN2 = initialAmbPressMinusWaterVapor * fractionOfN2InMix;                               /* initial inspired (alveolar) inert gas pressure in breathing mix */
//    double initialCompartmentPressN2 = initialAmbPressMinusWaterVapor * startPPN2;                               /* initial compartment inert gas pressure */
//    double rateIspiredVsAbmientN2 = descentRate * fractionOfN2InMix;                                  /* rate of change in inspired gas pressure with change in ambient pressure */
//    double halfTimeConstN2 = log(2.0)/4;                                       /* same as above for N2 */
//    
//    double pHe = initialInspiredGasPressHe + rateIspiredVsAbmientHe*(timeInterval-1/halfTimeConstHe)
//                    - (initialInspiredGasPressHe - initialCompartmentPressHe - (rateIspiredVsAbmientHe/halfTimeConstHe))*exp(-halfTimeConstHe * timeInterval);
//    
//    double pN2 = initialInspiredGasPressN2 + rateIspiredVsAbmientN2*(timeInterval-1/halfTimeConstN2)
//                    - (initialInspiredGasPressN2 - initialCompartmentPressN2 - (rateIspiredVsAbmientN2/halfTimeConstN2))*exp(-halfTimeConstN2 * timeInterval);
//    
//    NSLog(@"The Helium tissue pressure is %f", pHe);
//    NSLog(@"The Nitrogen tissue pressure is %f", pN2);
//    
//    double fHe = pHe + (((finalDepth - 2.0420) * fractionOfHeInMix)- pHe)* (1 - pow(2, (-10/1.51)));
//    
////    double fN2 = pN2 + (((finalDepth - 2.0420) * fractionOfN2InMix)- pN2)* (1 - pow(2, (-10.0/4.0)));
//    
//    double fN2 = .79 + (3.16 - 0.79) * (1.0 - pow(2, (-20.0/27.0)));
//
//    NSLog(@"The Helium tissue pressure after 20 minutes is %f", fHe);
//    NSLog(@"The Nitrogen tissue pressure*** after 10 minutes is %f", fN2);
//    

//    [diveObj release];

    [decoObj release];
    [diveParams release];
    [mix release];

    [pool drain];
    return 0;
}
